package com.lgj.demo0812.TestDemo;

import org.springframework.util.StopWatch;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author lizy51
 * @date 2022-10-11 16:03
 * dec:批量数据生成
 */
public class AddBatch {
    public static void main(String[] args) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String[] type = {"red", "pink", "blue", "green"};
        String[] make = {"wuhan", "jiangxi", "hubei"};
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String filePath;
        File file;
        FileOutputStream out = null;
        int count = 1;
        for (int i = 0; i < 100; i++) {
            if (i % 100 == 0) {
                if (out != null) {
                    out.close();
                }
//                filePath = "D:\\idea\\demo\\src\\main\\resources\\es\\test" + count + ".json";
                filePath = "/Users/liguangjiang/devTools/dataFile/demo"+count+".json";
                file = new File(filePath);
                out = new FileOutputStream(file, false);
                count++;
            }
            Date dNow = new Date();
            String str = "{\"index\":{\"_index\":\"test2\",\"_id\":"+(i+1)+"}}\n" +
                    "{\"name\" : \"java test " + (i+1)+ "\", \"id\" : " + (i+1) + ", \"datetime\" : \"" + ft.format(dNow) + "\", \"type\": \"" + type[new Random().nextInt(4)] + "\", \"make\": \"" + make[new Random().nextInt(3)] + "\"}\n";
            out.write(str.getBytes(Charset.forName("utf-8")));
        }
        out.close();
        stopWatch.stop();
        System.out.printf("执行时长：%g 秒.%n", stopWatch.getTotalTimeSeconds()); // %n 为换行®
    }

}
